	package {
	import MLP.mlp_effectLayer;
	
	import SEApp.dialogs.se_alert;
	import SEApp.dialogs.se_dialog;
	import SEApp.se_app;
	import SEApp.se_sapp;
	import SEApp.se_util;
	
	import UIFree.ObjectEvent;
	import UIFree.command.uif_commandManager;
	import UIFree.components.uif_shortKeyManager;
	import UIFree.components.uif_window;
	import UIFree.debug.uif_mouseInfo;
	import UIFree.uif_Timer;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	import UIFree.uif_loader;
	import UIFree.uif_styles;
	
	import UIUtils.uif_MLPEncoder;
	import UIUtils.uif_util;
	import UIUtils.uif_utilA;
	import UIUtils.uif_utilS;
	
	import classes.Commands.c_applyBrush;
	import classes.Commands.c_enterBrushMode;
	import classes.Commands.c_insertBitmapCommand;
	import classes.c_cursors;
	import classes.c_events;
	import classes.c_imageTypes;
	import classes.c_layerAdjustDialogs;
	import classes.c_locale;
	import classes.c_styles;
	import classes.c_uiEvents;
	import classes.c_urls;
	import classes.c_util;
	import classes.menu.c_defaultMenu;
	
	import components.CInterface.ICollages;
	import components.CInterface.IWindow;
	import components.brush.c_brushRelease;
	import components.c_basePortBar;
	import components.c_component;
	import components.c_cursor;
	import components.c_moduleBoxWindow;
	import components.c_pcomponent;
	import components.c_toolBar;
	import components.c_toolsLayer;
	import components.c_window;
	import components.dialogs.c_propertyDialog;
	import components.editor.c_baseElement;
	import components.editor.c_bitmapElement;
	import components.editor.c_clothingEditor;
	import components.editor.c_collagesEditor;
	import components.mainMenu.c_itemMenuEffect;
	import components.player.c_playerWindow;
	import components.scanario.c_assistantArea;
	import components.scanario.c_assistantAreaV;
	import components.scanario.c_scenarioManager;
	import components.scanario.elements.c_commandElem;
	import components.scanario.elements.c_eventElement;
	import components.scanario.elements.c_scenarioElem;
	import components.tools.c_layersWindow;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.geom.PerspectiveProjection;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import mx.utils.object_proxy;
	
	public class collages extends se_sapp implements ICollages {
		public static const TOOLSLAYERINDEX: int = 1;
		
		private var _cursor			: c_cursor;
		private var _classes		: Object;
		private var _plugins		: Object;
		private var _toolsLayer		: c_toolsLayer;
		
		public function collages() {
			stage.stageFocusRect = false;
			 
			mlp_effectLayer.URLPATH = c_urls.EFFECTURL;
			
			super(stage.stageWidth, stage.stageHeight);
			uif_loader.disp = workspace;
			listenEvents();
			
			_classes = uif_util.union(super.classes, {
				locale				: c_locale,
				itemMenuEffect		: c_itemMenuEffect
			});
		}
		
		override protected function initialize(): void {
			super.initialize();
			new uif_shortKeyManager(this);
			initializePlugins(
				createComponents
			);
			
			ExternalInterface.addCallback('jsGate', jsGate);
			_plugins = new Object();
		}
		
		public function get flashVars(): Object {
			return stage.loaderInfo.parameters;
		}
		
		protected function jsGate(params: Object): void {
			if (params.method) this[params.method](params.params); 
		}
		
		override protected function updateBounds():uif_component {
			super.updateBounds();
			updateCenterPP();
			return this;
		}
		
		public function get editorLayer(): uif_ecomponent {
			return layers[0];
		}
		
		protected function updateCenterPP(): void {
			var pp: PerspectiveProjection = new PerspectiveProjection();
			pp.projectionCenter = center;
			pp.fieldOfView = 30;
			transform.perspectiveProjection = pp;			
		}
		
		protected function listenEvents(): void {
			stage.addEventListener(Event.RESIZE, doStageResize);
			workspace.addEventListener(c_uiEvents.FULLSCREEN, doFullScreen);
//			workspace.addEventListener(c_events.MERGEMENU, doMergeMenu);
		}
		
		protected function doFullScreen(e: Event): void {
			if (stage.allowsFullScreen) {
				stage.displayState = (stage.displayState==StageDisplayState.FULL_SCREEN_INTERACTIVE)?
						StageDisplayState.NORMAL:StageDisplayState.FULL_SCREEN_INTERACTIVE;
				
			}
		}
		
		
		public function noCurrentLayerCreate(okFunc: Function): void {
			alert(This.classes.locale.WARNING, This.classes.locale.NOCURRENTLAYERCREATE, okFunc);
		}
		
		public function noCurrentLayer(): void {
			alert(This.classes.locale.WARNING, This.classes.locale.NOCURRENTLAYER);
		}
		
		public function getValueEvent(event: String, options: Object=null): Object {
			var obj: Object = uif_util.union({
				result: null
			}, options);
			workspace.dispatchEvent(new ObjectEvent(event, obj));
			return obj.result;
		}
/*		
		protected function doMergeMenu(e: ObjectEvent): void {
			c_defaultMenu.list = uif_utilA.union(c_defaultMenu.list(), e.object) as Array;
		}
*/		
		
		public function getExifHeader(): Object {
			return {
				SOFTWARE	: _classes.locale.APPNAME,
				SUBJECT		: uif_utilS.translit(_classes.locale.APPNAME)
			};
		}
		
		public function question(question: String, okFunc: Function, cancelFunc: Function=null, a_title: String=''): void {
			a_title = a_title?a_title:_classes.locale.MESSAGE;
			var alert: se_alert = alert(a_title, question, okFunc, true);
			if (cancelFunc != null)
				alert.cancelButton.addEvent(MouseEvent.CLICK, function(e: Event): void {
					cancelFunc();
				});
		}
		
		override protected function initLayers(layerCount:uint=6):void {
			_layers = new Array();
			for (var i: int =0; i<layerCount-1; i++) {
				var layerClass: Class = c_pcomponent;
				if (i == TOOLSLAYERINDEX) layerClass = c_toolsLayer;
				_layers[i] = new layerClass(this);
			}
			_toolsLayer = _layers[TOOLSLAYERINDEX];
		}
		
		override public function get classes():Object {
			return _classes;
		}
		
		protected function get toolsLayer(): c_toolsLayer {
			return _toolsLayer;
		}
		
		public function get workArea(): Rectangle {
			return getWorkArea();
		}
		
		protected function getWorkArea(): Rectangle {
			return getInner(0, c_toolBar.HEIGHT);
		}
		
		protected function createComponents(): void {
			new c_collagesEditor(editorLayer);	//uif_util.EnterHere(workArea, c_data.DEFAULTEDITORSIZE, true).size);
//			new c_clothingEditor(editorLayer);
			_cursor = new c_cursor(this, c_cursors);
			new c_assistantAreaV(this, 'assistantArea');
			
			if (flashVars.start_event) workspace.dispatchEvent(new Event(flashVars.start_event));
		}
		
		public function openFileURL(parent: c_component, fileURL: String): void {
			function doComplete(e: Event): void {
				c_imageTypes.loadBytes(workspace, (e.target as URLLoader).data as ByteArray, c_util.fileExt(fileURL));
			}
			
			var loader: URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, doComplete);
			loader.dataFormat = URLLoaderDataFormat.BINARY;
			
			loader.load(new URLRequest(fileURL));			
		}		
		
		public function get workspace(): EventDispatcher {
			return this;
		}
		
		override public function dispatchEvent(event:Event): Boolean {
			var canDE: Object = {
				type	: event.type,
				result	: true
			};
			super.dispatchEvent(new ObjectEvent(c_events.CANDISPATCHEVENT, canDE));
			if (!canDE.result) return false;
			ExternalInterface.call('WorkspaceBroadcast', event.type);
			return super.dispatchEvent(event); 
		}
		
		protected function initializePlugins(afterProc: Function): void {
/*			
			workspace.addEventListener(c_plugins.COMPLETESTARTPLUGINS, function(e: Event): void {
				createComponents();
			});
			new c_plugins(workspace, pluginList);
*/			
			afterProc();
		}
		
		public function pluginLoad(pluginURL: String): void {
			var url: String = pluginURL;
			if (flashVars.dev) url += '?v=' + Math.round(Math.random() * 100000);
			if (!_plugins.hasOwnProperty(pluginURL)) {
				function onLoadPlugin(e: Event): void {
					(e.target as LoaderInfo).removeEventListener(Event.INIT, onLoadPlugin);
				}
				_plugins[pluginURL] = new uif_loader(this, url, onLoadPlugin);
			} else {
				var loader: uif_loader = _plugins[pluginURL] as uif_loader;
				uif_util.waitCall(function(): Boolean {
					return loader.loaded;
				}, function(): void {
					(loader.contentLoaderInfo.content as Object).activate();
				});
			}
		}
		
/*		
		public function get pluginList(): Array {
			return ExternalInterface.call('pluginList');
		}
*/		
		
		protected function doStageResize(e: Event): void {
			resize(stage.stageWidth, stage.stageHeight);
		}
		
		override protected function getStyles():uif_styles {
			return new c_styles();
		}
		
		public function alert2(a_title: String, a_message: String): void {
			alert(a_title, a_message);
		}

		public function createDataModuleDialog(a_title: String, a_data: Object, previewURL: String, 
											   selectEvent: Object, a_rows: int, a_width: int, a_height: int, modal: Boolean = false, 
											   a_styleClass: Object=null): IWindow {
			var dialog: c_moduleBoxWindow = new c_moduleBoxWindow(dialogLayer, a_data, 
				previewURL, selectEvent, a_rows, a_width, a_height, a_title, modal, 1, a_styleClass);
			return dialog;
		}
	}
}